package com.lin.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lin.dto.UserDTO;
import com.lin.entity.User;
import com.lin.mapper.UserBalanceMapper;
import com.lin.mapper.UserMapper;
import com.lin.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lin.vo.DataVO;
import lombok.extern.slf4j.Slf4j;
import netscape.javascript.JSObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 林伟鸿
 * @since 2021-11-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;



    QueryWrapper<User> wrapper;

    /**
     * 查找用户
     *
     * @param userName
     * @return
     */
    @Override
    public User findByUserName(String userName) {
//        wrapper = new QueryWrapper<User>();
//        wrapper.eq("user_name", userName);
//        User user = userMapper.selectOne(wrapper);
        return userMapper.findByUserName(userName);

    }

    /**
     * 登录验证功能
     *
     * @param userDTO
     * @return
     */
    @Override
    public int login(UserDTO userDTO) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        HttpSession session = request.getSession();
        int flag = 0;
        //获得当前的用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(userDTO.getUserName(), SecureUtil.md5(userDTO.getPassword()));
        try {
            subject.login(token);
            flag = 1;
//            wrapper.eq("user_name", userDTO.getUserName());
//            User user = userMapper.selectOne(wrapper);
            User user = userMapper.findByUserName(token.getUsername());
            session.setAttribute("user", user);
        } catch (UnknownAccountException e) {
            flag = 2;
        } catch (IncorrectCredentialsException e) {
            flag = 3;
        }
        return flag;
    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @Override
    public int register(User user) {
        User myUser = new User();
        BeanUtils.copyProperties(user, myUser, "password");
        //密码额外处理，采用md5加密
        myUser.setPassword(SecureUtil.md5(user.getPassword()));
        return userMapper.registerUser(myUser.getUserName(),myUser.getPassword(),myUser.getPhone(),myUser.getEmail(),myUser.getNickName());
    }

    /**
     * 用户修改密码
     * @param password
     * @param userId
     * @return
     */
    @Override
    public int updatePwd(String password, Integer userId) {
        wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        User user = userMapper.selectOne(wrapper);
        user.setPassword(SecureUtil.md5(password));
        return userMapper.updateById(user);
    }


}
